package communicate;

import intro.ThreadUtil;

/**
 * Join: wait & notify 的一种封装
 * 
 * @author insight
 * @since 2021/8/31
 */
public class JoinTest {
    public static void main(String[] args) {
        Thread pre = Thread.currentThread();
        
        for (int i = 0; i < 10; i++) {
            // 注入前一个线程
            Thread t = new Thread(new Domino(pre), String.valueOf(i));
            t.start();
            pre = t;
        }

        ThreadUtil.second(3);
        System.out.println(Thread.currentThread().getName() + " terminated.");
    }

    static class Domino implements Runnable {
        private Thread t;

        public Domino(Thread t) {
            this.t = t;
        }

        @Override
        public void run() {
            try {
                // 等待前一个线程结束
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " terminated.");
        }
    }
}